package com.bang.study;

import com.google.gson.Gson;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;


public class UdpTool {


    public static void sendToUDP(String json, String ip, int port) {
        try {
            DatagramSocket datagramSocket = new DatagramSocket();
            DatagramPacket datagramPacket = new DatagramPacket(json.getBytes(), json.getBytes().length, InetAddress.getByName(ip), port);
            //鍙戦??
            datagramSocket.send(datagramPacket);
            datagramSocket.close();
            Thread.sleep(1000);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
		/*String netWorkJson = getNetWorkJson();
		System.err.println(netWorkJson);
		UdpTool.sendToUDP(netWorkJson, "192.168.96.172", 6666);*/

        Gson gson = new Gson();
        //发送原soc卡
        Map<String, Object> mp = new HashMap<>();
        mp.put("ip", "192.168.119.102");
        mp.put("dstIps", "127.0.0.1");
        mp.put("socVersion", "111");
        mp.put("related_ip", "1.2.3.4");
        mp.put("Event_Table_Name", "soccardchange");
        String json = gson.toJson(mp);
        UdpTool.sendToUDP(json, "192.168.96.172", 6666);
        System.out.println("192.168.119.102" + ";原asset表soc卡id：" + "111" + "已发送至udp~~~");

        //发送同步过来的soc卡
        Map<String, Object> mp1 = new HashMap<>();
        mp1.put("ip", "192.168.119.102");
        mp1.put("dstIps", "127.0.0.1");
        mp1.put("socVersion", "19875354654");
        mp1.put("related_ip", "1.2.3.4");
        mp1.put("Event_Table_Name", "soccardchange");
        String json1 = gson.toJson(mp1);
        UdpTool.sendToUDP(json1, "192.168.96.172", 6666);
        System.out.println("192.168.119.102" + ";端推送过来的soc卡id：" + "19875354654" + "已发送至udp~~~");


    }

    private static String getNetWorkJson() {
        Gson gson = new Gson();
        Map<String, Object> mp = new HashMap<>();
        mp.put("ip", "192.168.119.102");
        mp.put("dstIps", "127.0.0.1");
        mp.put("socVersion", "111111");
        mp.put("related_ip", "2.2.2.2");
        mp.put("Event_Table_Name", "soccardchange");
        String json = gson.toJson(mp);
        return json;

    }


}
